package com.lvmama.rhino.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.rhino.entity.TwoWayTreeNode;
import com.lvmama.rhino.service.RelationShipService;
import com.lvmama.rhino.utils.PageCodeUtil;

@Controller
@RequestMapping("application")
public class ApplicationController {
	protected Logger logger=Logger.getLogger(this.getClass());

	@Autowired
	public void setRelationShipService(RelationShipService relationShipService) {
		this.relationShipService = relationShipService;
	}

	private RelationShipService relationShipService;

	@RequestMapping("ajaxData")
	@ResponseBody
	public TwoWayTreeNode ajaxData(@RequestParam("pageCode") String pageCode,
			@RequestParam("dateTime") String dateTime) throws IOException {

		DateTimeFormatter formater = DateTimeFormat
				.forPattern("yyyy-MM-dd HH:mm:ss");
		Date startTime = DateTime.parse(dateTime + " 00:00:00", formater)
				.toDate();
		Date endTime = DateTime.parse(dateTime + " 23:59:59", formater)
				.toDate();
		TwoWayTreeNode rootNode = new TwoWayTreeNode();
			rootNode = relationShipService.queryRelationShip(pageCode,
					startTime, endTime);
		return rootNode;
	}

	@RequestMapping("autoComplete")
	@ResponseBody
	public List<String> autoComplete(@RequestParam("term") String keyword) {
		Properties prop = PageCodeUtil.getProperties("/a.properties");
		List<String> list = new ArrayList<>();
		for (String key : prop.stringPropertyNames()) {
			String page = (String) prop.get(key);
			if (page.contains(keyword)) {
				list.add(page);
			}
		}

		return list;
	}

}
